/*
 * @version: 1.0
 * @Author: zhl
 * @Date: 2021-09-02 15:49:35
 * @LastEditors: zhl
 * @LastEditTime: 2022-01-26 09:28:26
 * @Description: file content
 */
#ifndef __TASK_MANAGER_H__
#define __TASK_MANAGER_H__

#include <unordered_map>
#include <string>
#include <list>
#include <iostream>
#include <memory>
#include <pthread.h>
#include <mutex>

#include "CConfigManager.h"
#include "TaskThreadManager.h"
#include "CThreadNotify.h"
#include "HandleThreadManager.h"
#include "CMosquittoManager.h"
#include "global_data_struct.h"

//std::unordered_map<接口id, std::unordered_map<dev_id,  任务参数>>
//如果性能比较差可换成其它方案
typedef std::unordered_map<int64_t, std::unordered_map<int64_t, TASK_PARAMS>> SPLIT_TASK_TYPE;
typedef std::shared_ptr<SPLIT_TASK_TYPE>  SPLIT_TASK_PTR;
//



class TaskManager{
    public:
        TaskManager();
        ~TaskManager();

        int  run_task(std::shared_ptr<CConfigManager> cm, std::shared_ptr<CMosquittoManager> mm);
        void run_cmd(std::shared_ptr<CConfigManager> cm, std::shared_ptr<CMosquittoManager> mm, TASK_PARAMS &param);
        void stop_task();
        void change_uart_mode();
        int  run_scene(std::shared_ptr<CConfigManager> cm, std::shared_ptr<CMosquittoManager> mm);
        
    private:
        bool c_is_run;
        bool c_is_change_uart_mode;
        SPLIT_TASK_PTR c_tasks;
        std::mutex c_run_mutex;
        std::unordered_map<std::string, int> c_interfaces;
        std::list<std::shared_ptr<TaskThreadManager>> c_ttms;
        std::unordered_map<std::string, std::shared_ptr<HandleThreadManager>> c_htms;
        

    private:
        int  assign_task(std::shared_ptr<CConfigManager> cm);
        int  save_relate_info(std::shared_ptr<CConfigManager> cm);
        void assign_interfaces(std::shared_ptr<CConfigManager> cm, std::shared_ptr<CMosquittoManager> mm);
        void run_handle_dev_items();
        static void *handle_thread(void *arg);
        std::shared_ptr<HandleThreadManager> generate_handle_thread(std::shared_ptr<CConfigManager> cm, std::shared_ptr<CMosquittoManager> mm, std::string addr,int comm_type);
};






#endif /* __TASK_MANAGER_H__ */